package labox.innovation.gameserver.timer;

import labox.innovation.gameserver.GameTimeController;

/**
 * 定时器控制类
 * 
 */
public final class TimerController {

	//private TimerQueue<Timer, Boolean> _tq = new TimerQueue<Timer, Boolean>();

	private TimerTask _tq = new TimerTask();

	public static TimerController getInstance() {
		return SingletonHolder._instance;
	}

	private TimerController() {
	}

	public int getSize() {
		return _tq.size();
	}

	public void addTimer(Timer timer) {
		_tq.put(timer, Boolean.TRUE);
	}

	public void removeTimer(Timer timer) {
		_tq.remove(timer);
	}

	public void run() {
		Timer timer;
		for (;;) {
			timer = _tq.peek();
			if (timer != null && timer.getActiveTime() - GameTimeController.getGameTicks() <= 0) {
				timer = _tq.poll();
				timer.post();
				if (timer.isAlive) {
					timer.putTimerTask();
				}
			} else
				break;

		}
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final TimerController _instance = new TimerController();
	}

	//	public static void main(String[] s) {
	//		getInstance();
	//		GameTimeController.getInstance();
	//
	//		Timer t = new Timer(5000, true, new Runnable() {
	//			public void run() {
	//				System.out.println("aaaaa");
	//			}
	//		});
	//
	//		t.start();
	//
	//		System.out.println("getInstance()._tq.size()1:" + getInstance()._tq.size());
	//
	//		//getInstance().removeTimer(t);
	//
	//		System.out.println("getInstance()._tq.size()2:" + getInstance()._tq.size());
	//
	//		new Thread(new Runnable() {
	//
	//			@Override
	//			public void run() {
	//				while (true) {
	//					getInstance().run();
	//					try {
	//						Thread.sleep(100);
	//					} catch (InterruptedException e) {
	//						// TODO Auto-generated catch block
	//						e.printStackTrace();
	//					}
	//				}
	//
	//			}
	//		}).start();
	//
	//		try {
	//			Thread.sleep(10000);
	//		} catch (InterruptedException e) {
	//			// TODO Auto-generated catch block
	//			e.printStackTrace();
	//		}
	//		t.cancel(true);
	//
	//	}
}
